package com.zuorenke.vertx.factory;

import com.zuorenke.vertx.config.RequestProperties;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.handler.CorsHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-16 11:54:45
 **/
@Component
@Slf4j
public class CrosHandlerFactory extends HandlerFactory {
	@Resource
	private RequestProperties requestProperties;
	
	@Override
	public Handler create(){
		Set<HttpMethod> methods = new HashSet<HttpMethod>();
		methods.add(HttpMethod.GET);
		methods.add(HttpMethod.POST);
		methods.add(HttpMethod.OPTIONS);
		methods.add(HttpMethod.PUT);
		methods.add(HttpMethod.DELETE);
		methods.add(HttpMethod.HEAD);
		return CorsHandler.create(requestProperties.getAllowedOriginPattern()).allowedMethods(methods);
	}
}
